package com.ebeitech.doorapp.domain.dao;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;

import com.ebeitech.doorapp.application.EbeiApplication;
import com.ebeitech.doorapp.db.CommonDBHelper;
import com.ebeitech.doorapp.db.DBChangeInterface;
import com.ebeitech.doorapp.db.dao.BaseDBDaoImpl;
import com.ebeitech.doorapp.domain.EMessage;
import com.ebeitech.library.constant.MessageConstants;
import com.ebeitech.library.util.CommonUtil;
import com.ebeitech.library.util.SPManager;
import com.ebeitech.library.util.StringUtil;
import com.ebeitech.library.util.TimeUtil;

import java.util.List;

/**
 * Created by alice on 2017/10/24.
 */
public class EMessageDao extends BaseDBDaoImpl<EMessage> implements DBChangeInterface {

	private static EMessageDao messageDao;

	public static EMessageDao getInstance() {

		if(messageDao == null || messageDao.dbHelper == null){
			messageDao = new EMessageDao();
		}
		return messageDao;
	}

	public EMessageDao() {
		super(CommonDBHelper.getDBHelper(), EMessage.class);
		CommonDBHelper.addToDaoManager(this);
	}

	public void handlerMessage(String message) {
		Context context = EbeiApplication.getContext();
		EMessage chatMessage = new EMessage();
		chatMessage.initWithJson(message);
		boolean isSendFromSelf = false;
		if (chatMessage != null) {
			chatMessage.setReadFlag(EMessage.READ_FLAG_UNREAD);
			insertOrUpdate(chatMessage);

			Intent intent = new Intent(MessageConstants.ACTION_MESSAGE_RECEIVE);
			intent.putExtra(MessageConstants.MESSAGE, chatMessage);
			context.sendBroadcast(intent);
		}

		// 如果是自己发的消息不要提示
		if (isSendFromSelf) {
			return;
		}
		// 提示至少间隔3秒才响铃一次
		String currentTime = TimeUtil.getCurrentTime();
		String msgRingTime = SPManager.getInstance(context).get("msgRingTime", "");
		if (!(StringUtil.isEmpty(msgRingTime)
				|| TimeUtil.calculateDiffTime(msgRingTime, currentTime, TimeUtil.UNIT_SECOND) >= 3)) {
			return;
		}
		SPManager.getInstance(context).put("msgRingTime", currentTime);

		CommonUtil.playVibrator(context);
		CommonUtil.playNotification(context);
	}

	public List<EMessage> getMessageList(){
		StringBuilder sb = new StringBuilder("select * from ");
		sb.append(tableName).append(" a INNER JOIN (select messageId,max(time) as sendTime from ").append(tableName).
				append(" group by typeId) b on (a.messageId = b.messageId and a.time = b.sendTime)");
		return queryList(sb.toString(), null);
	}

	public List<EMessage> getMessageListWithPage(String typeId, int currentPage, int pageSize){
		return queryList(false, null, "typeId=?", new String[]{ typeId }, null, null, "time desc LIMIT " + pageSize + " OFFSET " + pageSize * (currentPage - 1), null);
	}

	public EMessage getMessageById(String messageId){
		return query(null, "messageId=?", new String[]{messageId});
	}

	public void setMessageRead(String messageId){
		ContentValues contentValues = new ContentValues();
		contentValues.put("readFlag", EMessage.READ_FLAG_READ);
		update(contentValues,"messageId=?",new String[]{messageId});
	}

	public long setMessageReadByType(String typeId){
		ContentValues contentValues = new ContentValues();
		contentValues.put("readFlag","1");
		return update(contentValues,"typeId=?",new String[]{typeId});
	}

	public void clearData(){
		delete(null, null);
	}

	@Override
	public void DBChange() {
		messageDao = null;
	}

}
